package xin.nick.demo;

import com.netflix.hystrix.*;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Auther: Nick inickning@outlook.com
 * @Date: 2021/2/27 00:24
 * @Description:
 */
public class MyHystrixCommand extends HystrixCommand<String> {

    private final String name;
    private static final HystrixCommandKey GETTER_KEY = HystrixCommandKey.Factory.asKey("MyKey");

    public MyHystrixCommand(String name) {
//        super(HystrixCommandGroupKey.Factory.asKey("MyGroup"));

//        super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("MyGroup"))
//                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
//                        .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE
//                        )));

//        super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("MyGroup"))
//                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
//                        .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
//                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(10)
//                        .withMaxQueueSize(100).withMaximumSize(100)));

        super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("MyGroup"))
                .andCommandKey(GETTER_KEY));

        this.name = name;
    }

    protected String run() throws Exception {
//        return this.name + ":" + Thread.currentThread().getName();

//        Thread.sleep(1000 * 10);

        System.err.println("get data");
        return this.name + ":" + Thread.currentThread().getName();

    }

    @Override
    protected String getCacheKey() {
        return String.valueOf(this.name);
    }

    public static void flushCache(String name) {
        HystrixRequestCache.getInstance(GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear(name);
    }

    @Override
    protected String getFallback() {
        return "超时失败了";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        String result = new MyHystrixCommand("Nick").execute();
//        System.out.println(result);

//        Future<String> future = new MyHystrixCommand("NickN").queue();
//        System.out.println(future.get());

//        HystrixRequestContext context = HystrixRequestContext.initializeContext();
//        String result = new MyHystrixCommand("NickNN").execute();
//        System.out.println(result);
//        Future<String> future = new MyHystrixCommand("NickNN").queue();
//        System.out.println(future.get());
//        context.shutdown();

        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        String result = new MyHystrixCommand("NickNNN").execute();
        System.out.println(result);
//        MyHystrixCommand.flushCache("NickNNN");
        Future<String> future = new MyHystrixCommand("NickNNN").queue();
        System.out.println(future.get());

    }

}
